package singleLinkedListExercise;

import singlelinkedlist.PosNotLegalException;

public class MySingleLinkedList implements IMySingleLinkedList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;

    @Override
    public void addFirst(int val) {
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while( cur != null){
            System.out.print(cur.val);
            if (cur.next != null){
                System.out.print("->");
            }
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while( cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void addIndex(int pos, int val) {
        if (!posIndexLegal(pos)){
            throw new PosNotLegalException("pos位置"+pos+"不合法！");
        }

        if (pos == 0){
            addFirst(val);
            return;
        }


        if (pos == size()){
            addLast(val);
            return;
        }

        ListNode node = new ListNode(val);
        ListNode cur = head;
        while (pos - 1 > 0){
            cur = cur.next;
            pos--;
        }
        node.next = cur.next;
        cur.next = node;

    }

    @Override
    public boolean contains(int val) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int val) {
        if (head == null) return;
        if (!contains(val)) {
            System.out.println("要删除的val不存在！");
            return;
        }
        if (head.val == val){
            head = head.next;
            return;
        }

        ListNode cur = head;
        while (cur.next.val != val){
            cur = cur.next;
        }
        cur.next = cur.next.next;
    }

    @Override
    public void removeAllKey(int val) {
        if (head == null) return;
        if (!contains(val)){
            System.out.println("你要删除的val不存在！");
            return;
        }
        ListNode cur = head;
        while (cur.next != null){
            if (head.val == val) {
                head = cur.next;
                cur = cur.next;
            }else if (cur.next.val == val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        if (head.val == val) head = head.next;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null){
            ListNode del = cur;
            cur = cur.next;
            del.next = null;
        }
        head = null;
    }

    private boolean posIndexLegal(int pos){
        return pos <= size() && pos >= 0;
    }
}
